#ifndef TOGGLESWITCH_H
#define TOGGLESWITCH_H

#include <QAbstractButton>
#include <QPropertyAnimation>
#include <QColor>
#include <QEnterEvent>
#include <QEvent>

class ToggleSwitch : public QAbstractButton {
    Q_OBJECT
    Q_PROPERTY(qreal thumbPosition READ thumbPosition WRITE setThumbPosition)
    Q_PROPERTY(QColor trackColor READ trackColor WRITE setTrackColor)
    Q_PROPERTY(QColor trackActiveColor READ trackActiveColor WRITE setTrackActiveColor)
    Q_PROPERTY(QColor thumbColor READ thumbColor WRITE setThumbColor)
    Q_PROPERTY(QColor thumbActiveColor READ thumbActiveColor WRITE setThumbActiveColor)
    Q_PROPERTY(QColor borderColor READ borderColor WRITE setBorderColor)
    Q_PROPERTY(bool showText READ showText WRITE setShowText)
    Q_PROPERTY(QString onText READ onText WRITE setOnText)
    Q_PROPERTY(QString offText READ offText WRITE setOffText)
    Q_PROPERTY(bool locked READ isLocked WRITE setLocked)

public:
    explicit ToggleSwitch(QWidget *parent = nullptr);
    ~ToggleSwitch() override;

    // 尺寸建议
    QSize sizeHint() const override;
    QSize minimumSizeHint() const override;

    // 属性访问函数
    qreal thumbPosition() const;
    void setThumbPosition(qreal position);

    QColor trackColor() const;
    void setTrackColor(const QColor &color);

    QColor trackActiveColor() const;
    void setTrackActiveColor(const QColor &color);

    QColor thumbColor() const;
    void setThumbColor(const QColor &color);

    QColor thumbActiveColor() const;
    void setThumbActiveColor(const QColor &color);

    QColor borderColor() const;
    void setBorderColor(const QColor &color);

    bool showText() const;
    void setShowText(bool show);

    QString onText() const;
    void setOnText(const QString &text);

    QString offText() const;
    void setOffText(const QString &text);

    bool isLocked() const;
    void setLocked(bool locked);

protected:
    void paintEvent(QPaintEvent *event) override;
    void mousePressEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override; // 新增鼠标释放事件
    void enterEvent(QEvent *event) override;
    void leaveEvent(QEvent *event) override;
    void resizeEvent(QResizeEvent *event) override;

private slots:
    void onToggled(bool checked);

private:
    // 用于锁定状态下防止视觉反馈
    bool m_pressed; // 用于追踪按下状态
    void updateVisualAppearance();

    void updateThumbTargetPosition(bool checked);

private:
    qreal m_thumbPosition;  // 滑块当前位置
    double m_radius;        // 圆角半径

    // 颜色系统
    QColor m_trackColor;
    QColor m_trackActiveColor;
    QColor m_trackDisabledColor;
    QColor m_thumbColor;
    QColor m_thumbActiveColor;
    QColor m_borderColor;
    QColor m_textColor;
    QColor m_textActiveColor;

    // 文本配置
    bool m_showText;
    QString m_onText;
    QString m_offText;

    // 状态控制
    bool m_locked;     // 是否锁定
    bool m_hovering;   // 悬停状态

    QPropertyAnimation *m_animation;
};

#endif // TOGGLESWITCH_H
